/**
 *
 */
package com.elcom.emocbc.cbm;

import java.util.Date;
import java.util.concurrent.SynchronousQueue;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleTrigger;

import com.elcom.emocbc.util.ActionThread;
import com.elcom.emocbc.util.GeneratorSeq;

/**
 * @author tiamo114
 *
 */
public class BroadcastUnit extends ActionThread {

    private final Log LOG = LogFactory.getLog(BroadcastUnit.class);
    private SynchronousQueue<BroadcastDTO> q = null;
    private Scheduler sched = null;

    /**
     * @param name
     */
    public BroadcastUnit(String name, SynchronousQueue<BroadcastDTO> _q, Scheduler sch) {
        super(name);
        this.q = _q;
        this.sched = sch;
    }

    /*
     * (non-Javadoc)
     *
     * @see com.elcom.livescreen.util.ActionThread#action()
     */
    @Override
    protected void action() throws Exception {
        BroadcastDTO broadcast = this.q.take();

        LOG.info("------- Scheduling Jobs -------------------");

        int seqID = GeneratorSeq.getNextSeq();
        // Establish Job
        JobDetail job = new JobDetail("BroadcastJob" + seqID, "CellBroadcastJobs" + seqID, BroadcastJob.class);
        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put("broadcast", broadcast);
        jobDataMap.put("schedule", sched);
        jobDataMap.put("job_name", job.getName());
        jobDataMap.put("job_group", job.getGroup());
        job.setJobDataMap(jobDataMap);

        SimpleTrigger trigger = new SimpleTrigger("BroadcastTrigger" + seqID, "CellBroadcastJobs" + seqID, broadcast.getStartDate(), broadcast.getEndDate(),
                SimpleTrigger.REPEAT_INDEFINITELY, 60000L);

        Date ft = sched.scheduleJob(job, trigger);
        LOG.info(job.getFullName() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000
                + " seconds");
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * com.elcom.livescreen.util.ActionThread#onException(java.lang.Exception)
     */
    @Override
    protected void onException(Exception e) {
        LOG.error(this.getName() + " is onException: ", e);
    }

    /*
     * (non-Javadoc)
     *
     * @see com.elcom.livescreen.util.ActionThread#onExecuting()
     */
    @Override
    protected void onExecuting() throws Exception {
        LOG.info(this.getName() + " is onExecuting");
    }

    /*
     * (non-Javadoc)
     *
     * @see com.elcom.livescreen.util.ActionThread#onKilling()
     */
    @Override
    protected void onKilling() {
        LOG.info(this.getName() + " is onKilling");
    }

    /*
     * (non-Javadoc)
     *
     * @see com.elcom.livescreen.util.ActionThread#sleeptime()
     */
    @Override
    protected long sleeptime() throws Exception {
        return 1;
    }
}
